<?php
/**
 * @desc rul class file
 * @author nana
 * @date 2011
 *
 */
class NICED_URL 
{
	public static function buildURL($app, $params = array(), $protocol = false) 
	{
		$routers = LC::get('router');
		$path = array_search(strtolower($app), $routers);

		if (!$path) 
		{
			throw new NICED_Exception('build '.$app.' error, can not find path: '.$path);
		}
		
		$query = trim($path);
		$p = array();

		foreach ($params as $key => $value) 
		{
			if (is_int($key)) 
			{
				$query .= '/'.$value;
			} 
			else 
			{
				$p[] = urlencode($key).'='.urlencode($value);
			}
		}

		if (count($p)) 
		{
			$query .= '?'.implode('&', $p);
		}

		$url = rtrim(self::getBaseURL($protocol), '/')."/".$query;
        
        return $url;
	}

	//return base url 
	public static function getBaseURL($protocol = false, $add_index_page = true)
	{
		$site_path  = SC::get('board_config.site_path');
		$index_page = SC::get('board_config.index_page_name');

        $base_url = "";
        // add index.php
		if ($add_index_page)
		{
			$base_url = trim(trim($site_path, "/")."/".trim($index_page, "/"), '/');
		}
		else
		{
            $base_url = trim($site_path, '/');
		}

        if (!$base_url)
        {
            return $base_url;
        }

        //protocol
		if ($protocol)
		{
			$base_url = rtrim(SC::get("board_config.main_domain"), '/')."/".$base_url;
		}
		else
		{
			$base_url = "/".$base_url;
		}

		return $base_url;
	}

	public static function getPublicHtmlURL($protocol = false)
	{
		$site_path  = SC::get('board_config.site_path');
		$path       = SC::get('board_config.path_public_html');
        $url        = trim(trim($site_path, '/').'/'.trim($path, '/'), '/');
      
        if ($protocol)
        {
            $url = trim($domain, '/').'/'.trim($url, "/");
        }
        else if ($url)
        {
            $url = '/'.$url;
        }

        return $url;
	}

	public static function getStaticURL($protocol = false)
	{
		$site_path  = SC::get('board_config.site_path');
		$path       = SC::get('board_config.path_static');
        $url        = trim(trim($site_path, '/').'/'.trim($path, '/'), '/');
      
        if ($protocol)
        {
            $url = trim($domain, '/').'/'.trim($url, "/");
        }
        else if ($url)
        {
            $url = '/'.$url;
        }

        return $url;		
	}
    
    public static function getStaticBaseURL($type, $protocol = false)
    {
        $site_path  = SC::get('board_config.site_path');
        
        switch($type)
        {
            case 'css' :
            {
                $path = SC::get('board_config.path_css');
                $domain = SC::get("board_config.css_domain");
                break;
            }
            case 'js' : 
            {
                $path = SC::get('board_config.path_js');
                $domain = SC::get("board_config.js_domain");
                break;
            }
            case 'image' : 
            {
                $path = SC::get('board_config.path_image');
                $domain = SC::get("board_config.image_domain");
                break;
            }
            default : 
            {
                throw new NICED_Exception('获取static base url type error, not valid');
            }
        }

        $url        = trim(trim($site_path, '/').'/'.trim($path, '/'), '/');
        
        if ($protocol)
        {
            $url = trim($domain, '/').'/'.trim($url, "/");
        }
        else if ($url)
        {
            $url = '/'.$url;
        }

        return $url;
    }

    public static function buildJsURL($file, $protocol = false)
    {
        $base = NICED_URL::getJsBaseURL($protocol);
        $url  = NICED_URL::appendVersion(NICED_URL::append($base, $file));
        
        return $url;
    }

    public static function buildCssURL($file, $protocol = false)
    {
        $base = NICED_URL::getCssBaseURL($protocol);
        $url  = NICED_URL::appendVersion(NICED_URL::append($base, $file));
        
        return $url;
    }

    public static function buildImageURL($file, $protocol = false)
    {
        $base = NICED_URL::getImageBaseURL($protocol);
        $url  = NICED_URL::appendVersion(NICED_URL::append($base, $file));
        
        return $url;
    }

    public static function getJsBaseURL($protocol = false)
    {
        return self::getStaticBaseURL('js', $protocol);
    }

    
    public static function getCssBaseURL($protocol = false)
    {
        return self::getStaticBaseURL('css', $protocol);
    }

    public static function getImageBaseURL($protocol = false)
    {
        return self::getStaticBaseURL('image', $protocol);
    }

    public static function append($url, $str)
    {
        return rtrim($url)."/".ltrim($str, "/");
    }
  
    public static function appendVersion($url)
    {
        return rtrim($url, "/")."?".SC::get("board_config.version");
    }

    public static function appendToURL($in_url, $str) 
	{
		$str = ltrim($str, "&");
		$in_url .= preg_match('/\?/i', $in_url)
            ? '&'.$str
            : '?'.$str;
			
        return $in_url;
    }

}//end class
